package net.haveraid.dao;

import java.util.List;

import net.haveraid.data.Raid;

import org.hibernate.SessionFactory;
import org.joda.time.Interval;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class RaidDAOImpl implements RaidDAO {
	
	private static final Logger logger = LoggerFactory.getLogger(RaidDAOImpl.class);
	
	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public List<Raid> getRaids() {
		return sessionFactory.getCurrentSession().createQuery("from Raid").list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Raid> getRaids(Interval interval) {
		logger.info("Getting raids within interval");
		return sessionFactory.getCurrentSession().createQuery("from Raid as r where r.date >= :start AND r.date <= :end").setParameter("start", interval.getStart()).setParameter("end", interval.getEnd()).list();
	}
	
	public Raid get(Integer id) {
		return (Raid)sessionFactory.getCurrentSession().get(Raid.class, id);
	}
	
	public void save(Raid raid) {
		sessionFactory.getCurrentSession().saveOrUpdate(raid);
	}
}
